package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.Spu;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.mapper.SpuDetailMapper;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author 虎哥
 */
@Service
@Transactional
public class SpuDetailServiceImpl extends ServiceImpl<SpuDetailMapper, SpuDetail> implements SpuDetailService {
    @Autowired
    private SpuService spuService;
    @Autowired
    private SpecParamService paramService;
    @Override
    public List<SpecParamDTO> querySpecParams(Long id, Boolean searching) {
        SpuDetail spuDetail = getById(id);
        String specification = spuDetail.getSpecification();
        //转为Map
        Map<Long, Object> map = JsonUtils.toMap(specification, Long.class, Object.class);
        //根据spu_id查询spu
        Spu spu = spuService.getById(id);
        //根据spu查询到category_id
        //根据分类id查询规格参数
        List<SpecParamDTO> specParamDTOList = paramService.querySpecParams(spu.getCid3(), null, searching);
        for (SpecParamDTO specParamDTO : specParamDTOList) {
            specParamDTO.setValue(map.get(specParamDTO.getId()));
        }
        return specParamDTOList;
    }
}
